package com.company.nuwa.common.utils;

import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>description</p >
 *
 * @author YUNTAO
 * @version 1.0
 * @date 2020/07/24 17:00
 */
public class VersionUtils {

    /**
     * 判断是否为较新的版本
     *
     * @param baseVersion
     * @param yourVersion
     * @return
     */
    public static Boolean isLaterVersion(String baseVersion, String yourVersion) {
        if (ObjectUtils.isEmpty(baseVersion) || ObjectUtils.isEmpty(yourVersion)) {
            return false;
        }
        String[] base = baseVersion.split("\\.");
        String[] your = yourVersion.split("\\.");
        if (base == null || base.length != 3 || your == null || your.length != 3) {
            return false;
        }
        List<Integer> baseCodes = Arrays.stream(base).map(e -> Integer.parseInt(e)).collect(Collectors.toList());
        List<Integer> yourCodes = Arrays.stream(your).map(e -> Integer.parseInt(e)).collect(Collectors.toList());
        Boolean result = false;
        for (int i = 0; i < 3; i++) {
            if (yourCodes.get(i) > baseCodes.get(i)) {
                result = true;
                break;
            } else if (yourCodes.get(i).equals(baseCodes.get(i))) {
                continue;
            } else {
                result = false;
                break;
            }
        }
        return result;
    }
}
